﻿var routineinspectionItemListTableVue = new Vue({
    el: "#routineinspectionItemListTable",
    data: {
        pageModel: {
            pageIdx: 1,
            pageSize: 10,
            pageTotal: 0,
            pageLength: 0,
            reults: []
        },
        searchModel: {
            queryName: "",
            queryTypeID: -1,
            queryIsDisable: -1,

            orderByType: false,
            isAsc: true,
            isAll: true
        },
        typeDictionary: {},
        isDisableDictionary: {}
    },
    methods: {
        AddItem: function () {
            AddRoutineInspectionItem(this);
        },
        EditItem: function (itemID) {
            EditRoutineInspectionItem(this, itemID);
        },
        DisableItem: function (itemID, isDisable) {
            DisableRoutineInspectionItem(this, itemID, isDisable);
        },
        DeleteItem: function (item) {
            DeleteRoutineInspectionItem(this, item);
        },
        GotoPage: function (toPageIdx) {
            this.pageModel.pageIdx = toPageIdx;
            ListRoutineInspectionItemForPage(this);
        },
        SearchItem: function () {
            this.pageModel.pageIdx = 1;
            this.searchModel.isAll = false;
            ListRoutineInspectionItemForPage(this);
        },
        SearchReset: function () {
            this.pageModel.pageIdx = 1;
            this.searchModel.queryName = "";
            this.searchModel.queryTypeID = -1;
            this.searchModel.queryIsDisable = -1;
            this.searchModel.isAll = true;
            ListRoutineInspectionItemForPage(this);
        }
        
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.typeDictionary = GlobalDictionary.GetEquipmentTypeDictionary();
        this.isDisableDictionary = GlobalDictionary.GetTrueOrFalseDictionary();
        ListRoutineInspectionItemForPage(this);
    }
});

/**
 * 查询所有点检项目
 */
function ListRoutineInspectionItemForPage(vue) {
    vue.pageModel.results = [];
    ShowAzLoading();

    $.ajax({
        type: "POST",
        url: "/RoutineInspection/ListRoutineInspectionItemForPage?v=" + Math.random(),
        data: {
            pageModel: vue.pageModel,
            searchModel: vue.searchModel
        },
        success: function (res) {
            if (res.status == "Success") {
                vue.pageModel = res.data;   
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}

/**
 *新增点检项目
 */
function AddRoutineInspectionItem(vue) {
    OpenAzModal("新增点检项目Add  routine check item", "/RoutineInspection/Page/ItemDetail?IsNew=true", function () {
        ListRoutineInspectionItemForPage(vue);
    });
}

/**
 * 更新点检项目是否禁用
 */
function DisableRoutineInspectionItem(vue, itemID, isDisable) {
    ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/RoutineInspection/DisableRoutineInspectionItem",
        data: { itemID: itemID, isDisable: isDisable },
        success: function (res) {
            if (res.status == "Success") {
                ListRoutineInspectionItemForPage(vue);
                toastr.success("更新成功Update succeeded。");
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}

/**
 * 编辑点检项目信息
 */
function EditRoutineInspectionItem(vue, itemID) {
    OpenAzModal("编辑点检项目Edit routine check item", "/RoutineInspection/Page/ItemDetail?ItemID=" + itemID, function () {
        ListRoutineInspectionItemForPage(vue);
    });
}

/**
 * 删除点检项目信息
 */
function DeleteRoutineInspectionItem(vue, item) {
    var message = "确认删除点检项目Confirm to delete the routine check item【" + item.Name + "】?";
    if (confirm(message)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/RoutineInspection/DeleteRoutineInspectionItem",
            data: { ItemID: item.ID },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除点检项目信息成功Routine check item information deleted successfully。");
                    ListRoutineInspectionItemForPage(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        })
    }
}